import {Ref, ref} from "vue";
import axios from "@/utils/axios";
import {message} from "ant-design-vue";

class CFGDeviceInfo{
    deviceId: number|null = null
    diameter: number|null = null
}

export default ()=>{
    const cfgDeviceViewInfo = ref({
        showModal:false
    })
    const cfgDeviceInfo = <Ref<CFGDeviceInfo>>ref(new CFGDeviceInfo())

    /**
     * 获取CFG设备详细信息
     * @param deviceId 设备id
     * @param callback 回调函数
     */
    const getCFGDeviceInfo = (deviceId:number, callback: Function|null = null)=>{
        cfgDeviceInfo.value = new CFGDeviceInfo();
        axios.post("/cfg/findDeviceParameterByDeviceId",{deviceId:deviceId}).then((res:any)=>{
            if(res.success){
                Object.assign(cfgDeviceInfo.value,res.data)
            }else{
                cfgDeviceInfo.value.deviceId = deviceId
            }
            if(callback) callback()
        })
    }

    /**
     * 设置CFG设备详细信息
     */
    const setCFGDeviceInfo = (callback:Function|null=null)=>{
        axios.post("/cfg/saveDeviceParameterByDeviceId",cfgDeviceInfo.value).then((res:any)=>{
            if(res.success){
                message.info("保存成功")
                if(callback) callback()
            }else{
                message.info(res.msg)
            }
        })
    }

    return {
        cfgDeviceViewInfo,
        cfgDeviceInfo,
        getCFGDeviceInfo,
        setCFGDeviceInfo
    }
}